from datetime import datetime
from pydantic import BaseModel, WithJsonSchema
from typing import Optional, List

# Pydantic 模型
from tortoise.queryset import QuerySet
from tortoise.contrib.pydantic import pydantic_model_creator
from models.models import (
    AccessLog, User, Userposts, Viewjilu, Tag, Post, PostDteta, PostDownload, Comment, SiteInfo, Order
)


class AdminPost(BaseModel):
    id: int
    imageSrc: str
    text: str
    title: str
    isTop: bool = False
    likes: int = 0
    comments_count: int = 0
    views: int = 0
    publishDate: str
    author: int  # 假设这里使用作者的 ID
    tags: int
    is_show: bool = True
    is_slider: bool = False
    post_detail: int

    class Config:
        from_attributes = True  # 用于支持从 ORM 对象创建


# AccessLog 模型
AccessLog_Pydantic = pydantic_model_creator(AccessLog, name="AccessLog")

# User 模型
User_Pydantic = pydantic_model_creator(User, name="User")

# Userposts 模型
Userposts_Pydantic = pydantic_model_creator(Userposts, name="Userposts")

# Viewjilu 模型
Viewjilu_Pydantic = pydantic_model_creator(Viewjilu, name="Viewjilu")

# Tag 模型
Tag_Pydantic = pydantic_model_creator(Tag, name="Tag")

# Post 模型
Post_Pydantic = pydantic_model_creator(Post, name="Post", exclude=("is_slider", "is_show", "isTop",))

admin_Post_Pydantic = pydantic_model_creator(Post, name="admin_Post")

# PostDteta 模型
PostDteta_Pydantic = pydantic_model_creator(PostDteta, name="PostDteta")

# PostDownload 模型
PostDownload_Pydantic = pydantic_model_creator(PostDownload, name="PostDownload")

# Comment 模型
Comment_Pydantic = pydantic_model_creator(Comment, name="Comment")

# SiteInfo 模型
SiteInfo_Pydantic = pydantic_model_creator(SiteInfo, name="SiteInfo")

Order_Pydantic = pydantic_model_creator(Order, name="order")


async def serialize_posts(posts: QuerySet) -> List[Post_Pydantic]:
    return [await Post_Pydantic.from_tortoise_orm(post) for post in posts]


def post_to_dict(post: Post) -> dict:
    return {
        'id': post.id,
        'is_slider': post.is_slider,
        'is_top': post.is_top,
        # 添加其他需要的字段
    }


class detail_id(BaseModel):
    id: int


class CommentCreateRequest(BaseModel):
    post_id: int
    content: str
    created_at: Optional[datetime] = None


class PaginationParams(BaseModel):
    page: int = 1
    page_size: int = 10


class PaymentRequest(BaseModel):
    pid: int
    trade_no: str
    out_trade_no: str
    type: str
    name: str
    money: float
    trade_status: str
    sign: str
    sign_type: str
